package com.anxinr.result;

import com.anxinr.enums.ExceptionEnum;
import com.anxinr.enums.ResultCode;
import lombok.Data;

import java.util.Objects;

/**
 * 统一的返回体参数
 *
 * @author anxinr
 */
@Data
public class Result<T> {
    private Integer code ;
    private String massage ;
    private T body ;

    public Result(Integer code, String massage, T body) {
        this.code = code;
        this.massage = massage;
        this.body = body;
    }

    public Result(Integer code, String massage) {
        this.code = code;
        if (Objects.isNull(code)){
            this.code = ExceptionEnum.ERROR_CODE ;
        }
        this.massage = massage;
    }

    public Result(ResultCode resultCode){
        this.code = resultCode.getCode() ;
        this.massage = resultCode.getMassage() ;
    }

    public Result(ResultCode resultCode, T body){
        this.code = resultCode.getCode() ;
        this.massage = resultCode.getMassage() ;
        this.body = body ;
    }
    public Result(T body){
        this.code = ResultCode.SUCCESS.getCode() ;
        this.massage = ResultCode.SUCCESS.getMassage() ;
        this.body = body ;
    }

    public Result(String massage, ResultCode fail) {
        this.code = fail.getCode() ;
        this.massage = massage ;
    }

    public static Result success(){
        return new Result(ResultCode.SUCCESS);
    }
    public Result success(T body){
        return new Result(ResultCode.SUCCESS,body);
    }

    public static Result fail(){
        return new Result(ResultCode.FAIL);
    }
    public static Result fail(String massage){
        return new Result(massage,ResultCode.FAIL);
    }
    public static Result fail(String massage,Integer code){
        return new Result(code, massage);
    }
}
